/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.fatec.ads.classes;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;
import org.hibernate.annotations.DiscriminatorFormula;

/**
 *
 * @author BRUNO
 */
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula(value="tp_local")
public abstract class Local extends AbstractTO{

    private LocalPK chaveComposta;
    private String nome;
    private int numero;
    private int tipoLocal;
    private List<Cartao> cartoes;

    //Construtor Padrao
    public Local() {
        this.chaveComposta = new LocalPK();
    }

    public Local(int discriminador) {
        this.chaveComposta = new LocalPK(discriminador);
    }

    public Local(int codigo, int discriminador){
        this.chaveComposta = new LocalPK(codigo, discriminador);
    }

    /**
     * @return the chaveComposta
     */
    @EmbeddedId
    public LocalPK getChaveComposta() {
        return chaveComposta;
    }

    /**
     * @param chaveComposta the chaveComposta to set
     */
    public void setChaveComposta(LocalPK chaveComposta) {
        this.chaveComposta = chaveComposta;
    }

    /**
     * @return the codigo
     */
    @Transient
    public int getCodigo() {
        return this.chaveComposta.getCodigo();
    }

    /**
     * @param codigo the codigo to set
     */
    public void setCodigo(int codigo) {
        this.chaveComposta.setCodigo(codigo);
    }

    /**
     * @return the nome
     */
    @Column(name="nm_local", length=100, nullable=false)
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the numero
     */
    @Column(name="cd_local", length=4)
    public int getNumero() {
        return numero;
    }

    /**
     * @param numero the numero to set
     */
    public void setNumero(int numero) {
        this.numero = numero;
    }

    /**
     * @return the tipoLocal
     */
    @Column(name="tp_local", nullable=false, length=1, insertable=false, updatable=false)
    public int getTipoLocal() {
        return tipoLocal;
    }

    /**
     * @param tipoLocal the tipoLocal to set
     */
    public void setTipoLocal(int tipoLocal) {
        this.tipoLocal = tipoLocal;
    }

    /**
     * @return the cartoes
     */
    @ManyToMany(fetch=FetchType.LAZY, mappedBy="locais")
/*    @JoinTable(name="LOCAL_CARTAO",
               joinColumns={@JoinColumn(name="cd_sequencial")},
               inverseJoinColumns=@JoinColumn(name="cd_cartao"))
 */
    public List<Cartao> getCartoes() {
        return cartoes;
    }

    /**
     * @param cartoes the cartoes to set
     */
    public void setCartoes(List<Cartao> cartoes) {
        this.cartoes = cartoes;
    }

    @Override
    @Transient
    public Serializable getPK() {
        return this.getChaveComposta();
    }

    @Override
    public void setPK(Serializable identifier) {
        this.setChaveComposta((LocalPK) identifier);
    }

    @Override
    public String toString() {
        return "Local[codigo=" + this.getCodigo() +
                        ", nome=" + this.getNome() +
                        ", numero=" + this.getNumero() + "]";
    }

}